package com.example.administrator.about_huanxin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.model.UserEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/23 0023.
 */

public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {

    private List<UserEntity> mDatas;

    private LayoutInflater inflater;

    private Context context;

    public OnSelectUserListener onSelectUserListener;

    public void setOnSelectUserListener(OnSelectUserListener onSelectUserListener){
        this.onSelectUserListener = onSelectUserListener;
    }

    public interface OnSelectUserListener{
        void onSelect(UserEntity userEntity);
    }

    public SearchAdapter(Context context, List<UserEntity> mDatas) {
        this.mDatas = mDatas;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setmDatas(List<UserEntity> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_search, parent, false);
        SearchViewHolder holder = new SearchViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, final int position) {
        try{
            if (!TextUtils.isEmpty(mDatas.get(position).getIcon().getUrl())) {
                Picasso.with(context).load(mDatas.get(position).getIcon().getUrl()).into(holder.searchItemImg);
            }
            holder.searchItemName.setText(mDatas.get(position).getNickname());
            holder.searchItemTime.setText(mDatas.get(position).getCreatedAt());
            holder.searchItemBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onSelectUserListener != null){
                        onSelectUserListener.onSelect(mDatas.get(position));
                    }
                }
            });
        }catch (Exception e){
            Log.e("okok",e.toString());
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return mDatas==null?0:mDatas.size();
    }
}

class SearchViewHolder extends RecyclerView.ViewHolder {

    @Bind(R.id.search_item_img)
    ImageView searchItemImg;
    @Bind(R.id.search_item_name)
    TextView searchItemName;
    @Bind(R.id.search_item_time)
    TextView searchItemTime;
    @Bind(R.id.search_item_btn)
    Button searchItemBtn;

    public SearchViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}